﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace Spider
{
    class HttpRequestSyn : IRequest
    {
        //private Dictionary<Uri, CookieContainer> DicCookie = new Dictionary<Uri, CookieContainer>();
        private Encoding encoding;
        static CookieContainer cookie = null;

        public HttpRequestSyn()
        {
            this.encoding = Encoding.GetEncoding(DataHandler.Instance.CurrentRule.Charset);
            //DicCookie.Clear();
        }

        #region IRequestSource

        public PageClass GetPage(Uri uri)
        {
            if (uri == null)
                return null;

            if (string.IsNullOrEmpty(uri.ToString().Trim()))
                return null;

            Uri baseURL = new Uri("http://" + uri.Host);

            HttpWebRequest request = null;
            HttpWebResponse response = null;
            Stream stream = null;
            StringBuilder sb = new StringBuilder();
            PageClass newpageclass = new PageClass();
            newpageclass.URL = uri;
            if (uri.ToString().Equals("http://www.forever21.com/category.asp?catalog_name=FOREVER21&category_name=whatsnew_app&Page=1"))
                Console.WriteLine("Stop");
            try
            {
                request = GetRequest(uri);
                response = (HttpWebResponse)request.GetResponse();
                stream = response.GetResponseStream();
                int count;
                byte[] buf = new byte[10240];

                response.Cookies = cookie.GetCookies(uri);

                if (stream == null)
                    return null;

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return null;
                }

                if (!response.ContentType.ToLower().StartsWith("text/"))
                {
                    return null;
                }

                do
                {
                    count = stream.Read(buf, 0, buf.Length);

                    if (count != 0)
                    {
                        sb.Append(encoding.GetString(buf, 0, count));
                    }
                }
                while (count > 0);
            }
            catch (WebException ex)
            {
                Log.Instance.WriteException(ErrorMsg(uri.ToString(), ex.Message));
            }
            catch (Exception ex)
            {
                Log.Instance.WriteException(ErrorMsg(uri.ToString(), ex.Message));
            }
            finally
            {
                if (stream != null) stream.Close();
                if (response != null) response.Close();
            }
            newpageclass.html = sb.ToString();
            return newpageclass;
        }

        protected HttpWebRequest GetRequest(Uri uri)
        {
            Uri baseURL = new Uri("http://" + uri.Host);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Timeout = 60000;
            request.KeepAlive = true;
            request.AllowAutoRedirect = true;
            request.MaximumAutomaticRedirections = 2;
            request.ProtocolVersion = HttpVersion.Version11;
            request.Accept = "text/html";
            request.UserAgent = Spider.Properties.Settings.Default.UserAgent;
           
            if (cookie == null)
                cookie = new CookieContainer();
            
            request.CookieContainer = cookie;

            return request;
        }

        private string ErrorMsg(string URL, string Msg)
        {
            return String.Format("Error Message:{0}    Request URL:{1}  DateTime:{2}", Msg, URL, DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss"));
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            //DicCookie = null;
        }

        #endregion
    }
}
